<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-tabs v-model="activeName">
        <el-tab-pane label="角色管理" name="role">
          <!-- 角色管理 -->
          <el-button style="maring-bottom: 20px;" type="primary" @click="showDialog = true">+ 新增角色</el-button>
          <el-table
            v-loading="loading"
            :data="list"
            border
            style="width: 100%"
          >
            <el-table-column
              align="center"
              type="index"
              label="序号"
              width="180"
              :index="indexFn"
            />
            <el-table-column
              align="center"
              prop="name"
              label="角色名"
              width="180"
            />
            <el-table-column
              align="center"
              prop="description"
              label="描述"
            />
            <el-table-column
              label="操作"
              align="center"
            >
              <template v-slot="{row}">
                <el-button type="text" @click="showAssign(row)">分配权限</el-button>
                <el-button type="text" @click="showUpdata(row)">编辑</el-button>
                <el-button type="text" @click="del(row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-row
            type="flex"
            justify="end"
            style="margin-top: 10px;"
          >
            <!--
          background:分页带有背景
          total： 总共有多少条数据
          page-size: 每页显示多少条
          current-pagesize: 当前页
        -->
            <el-pagination
              background
              layout="prev, pager, next"
              :total="total"
              :page-size="pagesize"
              :current-page="page"
              @current-change="changeFn"
            />
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="company">
          <el-tab-pane label="公司信息">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyForm.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyForm.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyForm.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyForm.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tab-pane>
      </el-tabs>

      <!-- 新增角色窗口 -->
      <el-dialog
        :title="form.id ? '修改角色' : '新增角色'"
        :visible="showDialog"
        width="30%"
        @close="close"
      >
        <el-form
          ref="form"
          :model="form"
          label-width="80px"
          :rules="rules"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入角色名称" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="form.description" placeholder="请输入角色描述" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="close">取 消</el-button>
          <el-button type="primary" @click="addRole">确 定</el-button>
        </template>
      </el-dialog>
      <el-dialog
        width="30%"
        title="分配权限"
        :visible.sync="showAssignDialog"
      >
        <el-tree
          ref="tree"
          :data="permissionList"
          :props="defaultProps"
          default-expand-all
          show-checkbox
          check-strictly
          node-key="id"
        />
        <template #footer>
          <el-button @click="showAssignDialog=false">取消</el-button>
          <el-button type="primary" @click="assign">确定</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleList, delRole, addRole, editRole, getCompanyById, getRoleDetail, assignPermission } from '@/api/setting'
import { mapState } from 'vuex'
import { getPermissionList } from '@/api/permisson'
import { listToTree } from '@/utils'
export default {
  name: 'Setting',
  data() {
    return {
      activeName: 'role',
      // 存放角色列表数据
      list: [],
      total: 0,
      page: 1,
      pagesize: 10,
      // 加载效果
      loading: false,
      companyForm: {},
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称' }
        ],
        description: [
          { required: true, message: '请输入角色描述' }
        ]
      },
      showDialog: false,
      showAssignDialog: false,
      // 拥有的权限id
      permIds: [],
      // 权限列表
      permissionList: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 当前角色的id
      roleId: ''
    }
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  created() {
    this.getRoleList() // 获取角色列表
    this.getCompanyInfo()
  },
  methods: {
    async getRoleList() {
      this.loading = true
      const data = await getRoleList(this.page, this.pagesize)
      // console.log(rows)
      // console.log(data)
      this.list = data.rows
      this.total = data.total
      this.loading = false
    },
    changeFn(value) {
      // console.log('----', value)
      this.page = value
      this.getRoleList()
    },
    indexFn(index) {
      return (this.page - 1) * this.pagesize + index + 1
    },
    async del(id) {
      // console.log(id)
      try {
        await this.$confirm('亲，确定要删除改角色嘛', '温馨提示', { type: 'warning' })
      } catch {
        return
      }

      // 发送请求。进行删除
      await delRole(id)
      this.$message.success('删除角色成功')
      // 重新渲染
      if (this.list.length === 1) {
        // 当前页只有一条数据
        this.page--
      }
      this.getRoleList()
    },
    async addRole() {
      // 对整个表单进行校验
      // console.log('----')
      try {
        await this.$refs.form.validate()
      } catch {
        return false
      }
      // 发送请求
      if (this.form.id) {
        await editRole(this.form)
      } else {
        await addRole(this.form)
        this.total++
        this.page = Math.ceil(this.total / this.pagesize)
      }
      // 添加成功
      this.$message.success('添加成功')
      // console.log('----')
      // 重新渲染
      this.getRoleList()
      // 关闭弹框
      this.showDialog = false
      // 把page改为最后一页 30
    },
    closeDialog() {
      this.showDialog = false
      // 重置表单数据
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    close() {
      this.showDialog = false
      this.form = {
        name: '',
        description: ''
      }
      this.$refs.form.resetFields()
    },
    showUpdata(row) {
      this.showDialog = true
      // 1. 发送请求 或者 直接使用row中的数据
      this.form = { ...row }
    },
    async getCompanyInfo() {
      const data = await
      getCompanyById(this.userInfo.companyId)
      this.companyForm = data
    },
    async showAssign(row) {
      // console.log(row)
      this.showAssignDialog = true
      // console.log('-----')
      const data = await getPermissionList()
      // console.log(data)
      this.permissionList = listToTree(data, '0')
      // console.log(this.permissionList)
      // 发送请求，获取角色已有的权限信息
      const { permIds } = await getRoleDetail(row.id)
      // console.log(permIds)
      this.$refs.tree.setCheckedKeys(permIds)
      // 保存当前角色id
      this.roleId = row.id
    },
    async assign() {
      // 获取到选中的权限列表
      const permIds = this.$refs.tree.getCheckedKeys()
      // console.log(permIds)
      await assignPermission(this.roleId, permIds)
      // console.log(111)

      // 提示消息
      this.$message.success('分配权限成功')
      this.showAssignDialog = false
    }
  }
}
</script>

<style>

</style>
